//vue公用js文件
import foot from '../../components/Common/foot'
import loading from '../../components/Common/loading'
import axios from 'axios'
axios.defaults.withCredentials=true;
const common={};
common.install=function (vue) {
    let pro = vue.prototype;
    /**
     * 全局注册组件
     */
    vue.component('foot',foot);
    vue.component('loading',loading);
    /**
     * 定义aixos
     */
    pro.axios=axios;

    /**
     * 获取屏幕高宽
     * @returns {{width: number, height: number}}
     */
    pro.screenInfo=function () {
        return {
            width:screen.width,
            height:screen.height
        };
    };
    pro.loading = function (word='',resolve=null) {
        this.$store.commit('loading',{word,resolve});
    };
    /**
     * 检查值
     * @param val     检测的值
     * @param type    类型
     * return Boolean
     * */
    pro.checkValue = function(val,type,val1=null){
        switch (type){
            case 'text':
                if(val.trim() == ''){
                    return false;
                }else{
                    return true
                }
                break;
            case 'phone':
                if(!(/^1[34578]\d{9}$/.test(val))){
                    return false;
                }else{
                    return true;
                }
                break;
            case 'repsw':
                if(val == val1){
                    return true;
                }else{
                    return false;
                }
                break;
            case 'select':
                if(val == 0){
                    return false;
                }else{
                    return true;
                }
                break;
            case 'number':
                if(val > 0){
                    return  true;
                }else{
                    return false;
                }
        }
    };
    /**
     *   检测用户登录是否有效
     * */
    pro.checkLogin = function () {
        let token;
        if(JSON.parse(localStorage.getItem('myToken')) == null){
            token = '';
        }else{
            token = JSON.parse(localStorage.getItem('myToken')).token;
        }
        if(!token){
            let url = encodeURIComponent('https://super.wylkeji.com/check');
            location.href = 'https://www.wylkeji.com/wx_auth/index/index?scope=snsapi_userinfo&state=WYL&redirect_uri='+url;
        }else{
            let time1 = JSON.parse(localStorage.getItem('myToken')).expirse;
            let time2 = (new Date()).getTime();
            if(time1 - time2 < 0){
                let url = encodeURIComponent('https://super.wylkeji.com/check');
                location.href = 'https://www.wylkeji.com/wx_auth/index/index?scope=snsapi_userinfo&state=WYL&redirect_uri='+url;
            }
        }
    };
    /**
     *   设备检测
     * */
    pro.checkDevice = function () {
        let isReset = true;
        const u = navigator.userAgent;
        const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if (isAndroid){
            //     alert('android');
        }
        if (isiOS){
            document.body.addEventListener('focusin', () => {
                //软键盘弹出的事件处理
                isReset = false;
            });
            document.body.addEventListener('focusout', () => {
                //软键盘收起的事件处理
                isReset = true;
                setTimeout(() => {
                    //当焦点在弹出层的输入框之间切换时先不归位
                    if (isReset) {
                        window.scroll(0, 0);//失焦后强制让页面归位
                    }
                }, 300);
            });
        }
    }
};

export default common;